package com.baoli.umpush.controller;

import com.baoli.umpush.pojo.MsgBody;
import com.baoli.umpush.service.UmengService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@Slf4j
@RequestMapping("/umeng")
public class UmengController {

    @Autowired
    private UmengService umengService;


    @RequestMapping(value = "/send", method = RequestMethod.POST, consumes = "application/json")
    public @ResponseBody String send(@RequestBody MsgBody umengMsg) throws Exception {
        if (null == umengMsg || StringUtils.isBlank(umengMsg.getTitle()) || StringUtils.isBlank(umengMsg.getText())) {
            log.error("Umeng send has no content.");
            throw new Exception("Umeng send has no content.");
        }

        if (StringUtils.isBlank(umengMsg.getTicker())) {
            umengMsg.setTicker(umengMsg.getTitle());
        }

        umengService.push(umengMsg);

        return "OK";
    }
}
